package com.ruoyi.ctcx.controller;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.utils.uuid.IdUtils;
import com.ruoyi.ctcx.domain.TbInsuranceMain;
import com.ruoyi.ctcx.domain.TbInsuranceSettle;
import com.ruoyi.ctcx.domain.TbInsuranceSettleCase;
import com.ruoyi.ctcx.domain.vo.TbInsuranceSettlePayStatics;
import com.ruoyi.ctcx.service.ITbInsuranceMainService;
import com.ruoyi.ctcx.service.ITbInsuranceSettleCaseService;
import com.ruoyi.ctcx.service.ITbInsuranceSettleService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.ctcx.domain.TbInsuranceSettleCasePay;
import com.ruoyi.ctcx.service.ITbInsuranceSettleCasePayService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 保险理赔赔案支付信息Controller
 * 
 * @author ruoyi
 * @date 2024-11-21
 */
@RestController
@RequestMapping("/insurance/pay")
public class TbInsuranceSettleCasePayController extends BaseController
{
    @Autowired
    private ITbInsuranceSettleCasePayService tbInsuranceSettleCasePayService;

    @Autowired
    private ITbInsuranceSettleCaseService tbInsuranceSettleCaseService;

    @Autowired
    private ITbInsuranceSettleService tbInsuranceSettleService;

    @Autowired
    private ITbInsuranceMainService tbInsuranceMainService;

    /**
     * 查询保险理赔赔案支付信息列表
     */
    @PreAuthorize("@ss.hasPermi('insurance:pay:list')")
    @GetMapping("/list")
    public TableDataInfo list(TbInsuranceSettleCasePay tbInsuranceSettleCasePay)
    {
        startPage();
        List<TbInsuranceSettleCasePay> list = tbInsuranceSettleCasePayService.selectTbInsuranceSettleCasePayList(tbInsuranceSettleCasePay);
        return getDataTable(list);
    }

    /**
     * 导出保险理赔赔案支付信息列表
     */
    @PreAuthorize("@ss.hasPermi('insurance:pay:export')")
    @Log(title = "保险理赔赔案支付信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, TbInsuranceSettleCasePay tbInsuranceSettleCasePay)
    {
        List<TbInsuranceSettleCasePay> list = tbInsuranceSettleCasePayService.selectTbInsuranceSettleCasePayList(tbInsuranceSettleCasePay);
        ExcelUtil<TbInsuranceSettleCasePay> util = new ExcelUtil<TbInsuranceSettleCasePay>(TbInsuranceSettleCasePay.class);
        util.exportExcel(response, list, "保险理赔赔案支付信息数据");
    }

    /**
     * 获取保险理赔赔案支付信息详细信息
     */
    @PreAuthorize("@ss.hasPermi('insurance:pay:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(tbInsuranceSettleCasePayService.selectTbInsuranceSettleCasePayById(id));
    }

    /**
     * 新增保险理赔赔案支付信息
     */
    @PreAuthorize("@ss.hasPermi('insurance:pay:add')")
    @Log(title = "保险理赔赔案支付信息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody TbInsuranceSettleCasePay tbInsuranceSettleCasePay)
    {
        //新增支付信息
        tbInsuranceSettleCasePay.setCreateBy(getUsername());
        tbInsuranceSettleCasePayService.insertTbInsuranceSettleCasePay(tbInsuranceSettleCasePay);

        //更新赔案和理赔信息
        updateSettleAndCase(tbInsuranceSettleCasePay);

        return toAjax(1);
    }

    /**
     * 修改保险理赔赔案支付信息
     */
    @PreAuthorize("@ss.hasPermi('insurance:pay:edit')")
    @Log(title = "保险理赔赔案支付信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody TbInsuranceSettleCasePay tbInsuranceSettleCasePay)
    {

        //修改支付信息
        tbInsuranceSettleCasePay.setUpdateBy(getUsername());
        tbInsuranceSettleCasePayService.updateTbInsuranceSettleCasePay(tbInsuranceSettleCasePay);

        //更新赔案和理赔信息
        updateSettleAndCase(tbInsuranceSettleCasePay);

        return toAjax(1);
    }

    /**
     * 删除保险理赔赔案支付信息
     */
    @PreAuthorize("@ss.hasPermi('insurance:pay:remove')")
    @Log(title = "保险理赔赔案支付信息", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {

        for (Long id : ids)
        {
            TbInsuranceSettleCasePay tbInsuranceSettleCasePay = tbInsuranceSettleCasePayService.selectTbInsuranceSettleCasePayById(id);

            //逻辑删除支付信息
            tbInsuranceSettleCasePayService.deleteTbInsuranceSettleCasePayById(id);

            //更新赔案和理赔信息
            updateSettleAndCase(tbInsuranceSettleCasePay);
        }
        return toAjax(0);
    }

    /**
     * 更新赔案和理赔信息
     * @param tbInsuranceSettleCasePay
     */
    private void updateSettleAndCase(TbInsuranceSettleCasePay tbInsuranceSettleCasePay)
    {
        //更新赔案信息
        TbInsuranceSettleCase tbInsuranceSettleCase =  tbInsuranceSettleCaseService.selectTbInsuranceSettleCaseById(tbInsuranceSettleCasePay.getCaseId());

        if (null != tbInsuranceSettleCase)
        {
            TbInsuranceSettlePayStatics  allPay = tbInsuranceSettleCasePayService.selectSettlePayStaticsByCaseId(tbInsuranceSettleCase.getId());

            //回写赔案信息
            tbInsuranceSettleCase.setCompleteMoney(allPay.getPayAmount());
            tbInsuranceSettleCase.setUpdateBy(getUsername());
            tbInsuranceSettleCaseService.updateTbInsuranceSettleCase(tbInsuranceSettleCase);


            //更新理赔信息
            TbInsuranceSettle tbInsuranceSettle = tbInsuranceSettleService.selectTbInsuranceSettleById(tbInsuranceSettleCase.getSettleId());

            TbInsuranceSettlePayStatics  settlePay = tbInsuranceSettleCasePayService.selectSettlePayStaticsBySettleId(tbInsuranceSettle.getId());


            //获取保费
            TbInsuranceMain tbInsuranceMain = tbInsuranceMainService.selectTbInsuranceMainById(tbInsuranceSettle.getInsureId());

            if (null != tbInsuranceMain && null != tbInsuranceMain.getPremium())
            {
                BigDecimal premium =  tbInsuranceMain.getPremium();

                //已决赔付率:赔付赔款/保费

                BigDecimal  payAmount = settlePay.getPayAmount();

                BigDecimal hundred = new BigDecimal("100");

                BigDecimal rate = payAmount.multiply(hundred).divide(premium, 2, RoundingMode.HALF_UP);

                tbInsuranceSettle.setCompleteAmount(payAmount);

                tbInsuranceSettle.setRate(rate);

                //回写理赔信息
                tbInsuranceSettle.setUpdateBy(getUsername());
                tbInsuranceSettleService.updateTbInsuranceSettle(tbInsuranceSettle);

            }

        }
    }



    public static void main(String[] args)
    {

        BigDecimal hundred = new BigDecimal("100");
        BigDecimal insureAmount = new BigDecimal("100000");
        BigDecimal payAmount = new BigDecimal("1000");
        BigDecimal rate = payAmount.multiply(hundred).divide(insureAmount, 2, RoundingMode.HALF_UP);
        System.out.println(rate);
    }
}
